package logic

import (
	"context"

	"gitee.com/dn-jinmin/chatcms/internal/domain"
	"gitee.com/dn-jinmin/chatcms/internal/model"
	"gitee.com/dn-jinmin/chatcms/internal/svc"
)

type Speechcraft interface {
	List(ctx context.Context, req *domain.SpeechcraftListReq) (resp *domain.SpeechcraftListResp, err error)
	Info(ctx context.Context, req *domain.IdReq) (resp *domain.Speechcraft, err error)
	Create(ctx context.Context, req *domain.Speechcraft) (err error)
	Edit(ctx context.Context, req *domain.Speechcraft) (err error)
	Delete(ctx context.Context, req *domain.IdReq) (err error)
}

type speechcraft struct {
	svcCtx *svc.ServiceContext
}

func NewSpeechcraft(svcCtx *svc.ServiceContext) Speechcraft {
	return &speechcraft{
		svcCtx: svcCtx,
	}
}

func (l *speechcraft) List(ctx context.Context, req *domain.SpeechcraftListReq) (resp *domain.SpeechcraftListResp, err error) {
	resp = &domain.SpeechcraftListResp{}

	// 调用模型层的List方法
	results, err := l.svcCtx.SpeechcraftModel.List(ctx, req)
	if err != nil {
		return nil, err
	}

	// 转换为domain对象
	for _, item := range results {
		resp.List = append(resp.List, item.ToDomain())
	}

	return
}

func (l *speechcraft) Info(ctx context.Context, req *domain.IdReq) (resp *domain.Speechcraft, err error) {
	data, err := l.svcCtx.SpeechcraftModel.FindOne(ctx, req.Id)
	if err != nil {
		return nil, err
	}

	resp = data.ToDomain()
	return
}

func (l *speechcraft) Create(ctx context.Context, req *domain.Speechcraft) (err error) {
	data := model.SpeechcraftFromDomain(req)
	return l.svcCtx.SpeechcraftModel.Insert(ctx, data)
}

func (l *speechcraft) Edit(ctx context.Context, req *domain.Speechcraft) (err error) {
	data := model.SpeechcraftFromDomain(req)
	return l.svcCtx.SpeechcraftModel.Update(ctx, data)
}

func (l *speechcraft) Delete(ctx context.Context, req *domain.IdReq) (err error) {
	return l.svcCtx.SpeechcraftModel.Delete(ctx, req.Id)
}
